package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.RoleMenu;

@Repository
public interface RoleMenuDao extends JpaRepository<RoleMenu, Integer> {

    @Transactional
    @Modifying
    Integer deleteByRoleMenuId(Integer roleMenuId);

    List<RoleMenu> findByRoleMenuId(Integer roleMenuId);

    @Transactional
    @Modifying
    Integer deleteByRoleMenuIdIn(List<Integer> listId);

    //	@Query(nativeQuery = true, value = "select r.role_Id as roleId,r.roleName \r\n" + 
//			"from role r where r.role_Id not in (select rm.role_Id from role_menu rm)")
    @Query(nativeQuery = true, value = "select r.role_Id as roleId,r.roleName \r\n" +
            "from role r where r.role_Id ")
    List<Map<String, Object>> queryRoleNameNotAllocation();

    @Query(nativeQuery = true, value = "SELECT m.menuName,r.roleName,rm.* FROM (menus m INNER JOIN role_menu rm ON m.menu_Id = rm.menu_Id)\r\n" +
            "INNER JOIN role r ON r.role_Id = rm.role_Id")
    List<Map<String, Object>> queryAllRoleAndMenuInfo();

    @Query(nativeQuery = true, value = "SELECT m.menuName,r.roleName,rm.* FROM (menus m INNER JOIN role_menu rm ON m.menu_Id = rm.menu_Id)\r\n" +
            "INNER JOIN role r ON r.role_Id = rm.role_Id\r\n" +
            "WHERE r.roleName like CONCAT('%',:roleName,'%')")
    List<Map<String, Object>> findByRoleNameLike(@Param("roleName") String roleName);

    @Query(nativeQuery = true, value = "SELECT m.menuName,r.roleName,rm.* FROM (menus m INNER JOIN role_menu rm ON m.menu_Id = rm.menu_Id)\r\n" +
            "INNER JOIN role r ON r.role_Id = rm.role_Id\r\n" +
            "WHERE m.menuName like CONCAT('%',:menuName,'%')")
    List<Map<String, Object>> findByMenuNameLike(@Param("menuName") String menuName);


}
